import '../utils/common_device_info.dart';
import '../utils/deviceinfo_utils.dart';

class UserContext {

  static UserContext? _instance;

  static UserContext? cxt()=> _instance;

  UserContext._();

  CommonDeviceInfo? _deviceInfo;

  String _username = '';
  String _avatar = '';
  int _uid = 0;
  int _createTime = 0;

  int  get uid => _instance?._uid ?? 0;

  String get userName => _username;

  String get avatar => _avatar;


  static CommonDeviceInfo? get deviceInfo{
     if(_instance?._deviceInfo==null){
       DeviceInfo.getCommonDeviceInfo().then((value) => {
         _instance?._deviceInfo = value,
       });
     }
     return _instance!._deviceInfo;
  }

  factory UserContext.fromJson(Map<String, dynamic> json) {
    _instance ??= UserContext._();
    _instance!._username = json['userName'];
    _instance!._uid = json['userId'];
    _instance!._createTime = json['createTime'] ?? 0;
    _instance!._avatar = json['avatar'];
    _instance!._deviceInfo=deviceInfo;
    return _instance!;
  }
}
